package com.aquarium.ui;

import java.util.Observable;
import java.util.Observer;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.alarm.AlarmManager;
import com.aquarium.config.AlarmDefaultValue;

public class AlarmSettingActivity extends Activity implements OnClickListener,
		Observer {
	// AlarmValue alarmValue;
	AlarmManager mAlarmManager;
	String hour;
	String minute;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarmsetting);
		mAlarmManager = AlarmManager.getInstance(this);
		mAlarmManager.addObserver(this);
		hour = getResources().getString(R.string.hour);
		minute = getResources().getString(R.string.minute);
		initAllView();
		setOnListener();
	}

	private void initAllView() {
		initConductivityView();
		initPhView();
		initOxygenView();
		initTemperatureView();
		initWaterLevelView();
		initMailPhone();

	}

	private void setOnListener() {
		findViewById(R.id.resetcondetivity).setOnClickListener(this);
		findViewById(R.id.intervalcondutivityunit).setOnClickListener(this);
		findViewById(R.id.resetoxygen).setOnClickListener(this);
		findViewById(R.id.intervaloxygenunit).setOnClickListener(this);
		findViewById(R.id.resetph).setOnClickListener(this);
		findViewById(R.id.intervalphunit).setOnClickListener(this);
		findViewById(R.id.resettemperature).setOnClickListener(this);
		findViewById(R.id.intervaltemperatureunit).setOnClickListener(this);
		findViewById(R.id.resetwaterlevel).setOnClickListener(this);
		findViewById(R.id.intervalwaterlevelunit).setOnClickListener(this);

		findViewById(R.id.resetall).setOnClickListener(this);
		findViewById(R.id.iport).setOnClickListener(this);
		findViewById(R.id.export).setOnClickListener(this);
		findViewById(R.id.ok).setOnClickListener(this);
		findViewById(R.id.cancel).setOnClickListener(this);
	}

	private void saveAllValue() {
		saveConductivityValue();
		saveOxygenValue();
		savePhValue();
		saveTemperatureValue();
		saveWaterLevelValue();
		saveMailPhone();
	}

	private void initMailPhone() {

		((EditText) findViewById(R.id.editTextsms)).setText(mAlarmManager
				.getPhoneNum());
		((EditText) findViewById(R.id.editTextemsil)).setText(mAlarmManager
				.getEmialAddr());
		((CheckBox) findViewById(R.id.checkBoxsms)).setChecked(mAlarmManager
				.isPhoneEnable());
		((CheckBox) findViewById(R.id.checkBoxemail)).setChecked(mAlarmManager
				.isEmailEnable());
	}

	private void saveMailPhone() {
		mAlarmManager.setPhoneNum(((EditText) findViewById(R.id.editTextsms))
				.getText().toString());
		mAlarmManager
				.setEmialAddr(((EditText) findViewById(R.id.editTextemsil))
						.getText().toString());
		mAlarmManager
				.setPhoneEnable(((CheckBox) findViewById(R.id.checkBoxsms))
						.isChecked());
		mAlarmManager
				.setEmailEnable(((CheckBox) findViewById(R.id.checkBoxemail))
						.isChecked());
	}

	private void showItemView(int id1, float value1, int id2, float value2,
			int id3, int value3, int id4) {
		((TextView) findViewById(id1)).setText(value1 + "");
		((TextView) findViewById(id2)).setText(value2 + "");

		if (value3 > 59 && value3 % 60 == 0) {
			value3 = value3 / 60;
			((TextView) findViewById(id4)).setText(R.string.hour);
		} else {
			((TextView) findViewById(id4)).setText(R.string.minute);
		}

		((TextView) findViewById(id3)).setText(value3 + "");

	}

	private void initConductivityView() {
		int id1 = R.id.mincondutivity;
		int id2 = R.id.maxcondutivity;
		int id3 = R.id.intervalcondutivity;
		int id4 = R.id.intervalcondutivityunit;
		float value1 = mAlarmManager.getConductivityMin();
		float value2 = mAlarmManager.getConductivityMax();
		int value3 = mAlarmManager.getConductivityAlarmInterval();

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}

	private void showConductivityDefault() {
		int id1 = R.id.mincondutivity;
		int id2 = R.id.maxcondutivity;
		int id3 = R.id.intervalcondutivity;
		int id4 = R.id.intervalcondutivityunit;
		float value1 = AlarmDefaultValue.MINConductivity;
		float value2 = AlarmDefaultValue.MAXConductivity;
		int value3 = AlarmDefaultValue.INTERVALConductivity;

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}

	private void saveConductivityValue() {
		mAlarmManager.setConductivityMin(Float
				.valueOf(((TextView) findViewById(R.id.mincondutivity))
						.getText().toString()));
		mAlarmManager.setConductivityMax(Float
				.valueOf(((TextView) findViewById(R.id.maxcondutivity))
						.getText().toString()));
		float i = Float
				.valueOf(((TextView) findViewById(R.id.intervalcondutivity))
						.getText().toString());
		if (((TextView) findViewById(R.id.intervalcondutivityunit)).getText()
				.toString().equals(hour)) {
			i = i * 60;
		}
		mAlarmManager.setConductivityAlarmInterval((int) i);

	}

	private void initOxygenView() {
		((TextView) findViewById(R.id.minoxygen)).setText(mAlarmManager
				.getOxygenMin() + "");
		((TextView) findViewById(R.id.maxoxygen)).setText(mAlarmManager
				.getOxygenMax() + "");
		int i = mAlarmManager.getOxygenAlarmInterval();
		if (i > 59 && i % 60 == 0) {
			i = i / 60;
			((TextView) findViewById(R.id.intervaloxygenunit))
					.setText(R.string.hour);
		} else {
			((TextView) findViewById(R.id.intervaloxygenunit))
					.setText(R.string.minute);
		}
		((TextView) findViewById(R.id.intervaloxygen)).setText(i + "");
	}
	private void showOxygenDefault() {
		int id1 = R.id.minoxygen;
		int id2 = R.id.maxoxygen;
		int id3 = R.id.intervaloxygen;
		int id4 = R.id.intervaloxygenunit;
		float value1 = AlarmDefaultValue.MINOxygen;
		float value2 = AlarmDefaultValue.MAXOxygen;
		int value3 = AlarmDefaultValue.INTERVALOxygen;

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}
	private void saveOxygenValue() {
		mAlarmManager.setOxygenMin(Float
				.valueOf(((TextView) findViewById(R.id.minoxygen)).getText()
						.toString()));
		mAlarmManager.setOxygenMax(Float
				.valueOf(((TextView) findViewById(R.id.maxoxygen)).getText()
						.toString()));
		float i = Float.valueOf(((TextView) findViewById(R.id.intervaloxygen))
				.getText().toString());
		String str = ((TextView) findViewById(R.id.intervalcondutivityunit))
				.getText().toString();
		if (str.equals(hour)) {
			i = i * 60;
		}
		mAlarmManager.setConductivityAlarmInterval((int) i);

	}

	private void initPhView() {
		((TextView) findViewById(R.id.minph)).setText(mAlarmManager.getPhMin()
				+ "");
		((TextView) findViewById(R.id.maxph)).setText(mAlarmManager.getPhMax()
				+ "");
		int i = mAlarmManager.getPhAlarmInterval();
		if (i > 59 && i % 60 == 0) {
			i = i / 60;
			((TextView) findViewById(R.id.intervalphunit))
					.setText(R.string.hour);
		} else {
			((TextView) findViewById(R.id.intervalph)).setText(R.string.minute);
		}
		((TextView) findViewById(R.id.intervalph)).setText(i + "");
	}
	private void showPhDefault() {
		int id1 = R.id.minph;
		int id2 = R.id.maxph;
		int id3 = R.id.intervalph;
		int id4 = R.id.intervalphunit;
		float value1 = AlarmDefaultValue.MINPH;
		float value2 = AlarmDefaultValue.MAXPH;
		int value3 = AlarmDefaultValue.INTERVALPH;

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}

	private void savePhValue() {
		mAlarmManager.setPhMin(Float
				.valueOf(((TextView) findViewById(R.id.minph)).getText()
						.toString()));
		mAlarmManager.setPhMax(Float
				.valueOf(((TextView) findViewById(R.id.maxph)).getText()
						.toString()));
		float i = Float.valueOf(((TextView) findViewById(R.id.intervalph))
				.getText().toString());
		if (((TextView) findViewById(R.id.intervalphunit)).getText().toString()
				.equals(hour)) {
			i = i * 60;
		}
		mAlarmManager.setPhAlarmInterval((int) i);

	}

	private void initTemperatureView() {
		((TextView) findViewById(R.id.mintemperature)).setText(mAlarmManager
				.getTemperatureMin() + "");
		((TextView) findViewById(R.id.maxtemperature)).setText(mAlarmManager
				.getTemperatureMax() + "");
		int i = mAlarmManager.getTemperatureAlarmInterval();
		if (i > 59 && i % 60 == 0) {
			i = i / 60;
			((TextView) findViewById(R.id.intervaltemperatureunit))
					.setText(R.string.hour);
		} else {
			((TextView) findViewById(R.id.intervaltemperatureunit))
					.setText(R.string.minute);
		}
		((TextView) findViewById(R.id.intervaltemperature)).setText(i + "");
	}
	private void showTemperatureDefault() {
		int id1 = R.id.mintemperature;
		int id2 = R.id.maxtemperature;
		int id3 = R.id.intervaltemperature;
		int id4 = R.id.intervaltemperatureunit;
		float value1 = AlarmDefaultValue.MINTEMPERATURE;
		float value2 = AlarmDefaultValue.MAXTEMPERATURE;
		int value3 = AlarmDefaultValue.INTERVALTEMPERATURE;

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}
	private void saveTemperatureValue() {
		mAlarmManager.setTemperatureMin(Float
				.valueOf(((TextView) findViewById(R.id.mintemperature))
						.getText().toString()));
		mAlarmManager.setTemperatureMax(Float
				.valueOf(((TextView) findViewById(R.id.maxtemperature))
						.getText().toString()));
		float i = Float
				.valueOf(((TextView) findViewById(R.id.intervaltemperature))
						.getText().toString());
		if (((TextView) findViewById(R.id.intervaltemperatureunit)).getText()
				.toString().equals(hour)) {
			i = i * 60;
		}
		mAlarmManager.setTemperatureAlarmInterval((int) i);

	}

	private void initWaterLevelView() {
		((TextView) findViewById(R.id.minwaterlevel)).setText(mAlarmManager
				.getWaterLevelMin() + "");
		((TextView) findViewById(R.id.maxwaterlevel)).setText(mAlarmManager
				.getWaterLevelMax() + "");
		int i = mAlarmManager.getWaterLevelInterval();
		if (i > 59 && i % 60 == 0) {
			i = i / 60;
			((TextView) findViewById(R.id.intervalwaterlevelunit))
					.setText(R.string.hour);
		} else {
			((TextView) findViewById(R.id.intervalwaterlevelunit))
					.setText(R.string.minute);
		}
		((TextView) findViewById(R.id.intervalwaterlevel)).setText(i + "");
	}
	private void showWaterLevelDefault() {
		int id1 = R.id.minwaterlevel;
		int id2 = R.id.maxwaterlevel;
		int id3 = R.id.intervalwaterlevel;
		int id4 = R.id.intervalwaterlevelunit;
		float value1 = AlarmDefaultValue.MINWATERLEVEL;
		float value2 = AlarmDefaultValue.MAXWATERLEVEL;
		int value3 = AlarmDefaultValue.INTERVALWATERLEVEL;

		showItemView(id1, value1, id2, value2, id3, value3, id4);

	}

	private void saveWaterLevelValue() {
		mAlarmManager.setWaterLevelMin(Float
				.valueOf(((TextView) findViewById(R.id.minwaterlevel))
						.getText().toString()));
		mAlarmManager.setWaterLevelMax(Float
				.valueOf(((TextView) findViewById(R.id.maxwaterlevel))
						.getText().toString()));
		float i = Float
				.valueOf(((TextView) findViewById(R.id.intervalwaterlevel))
						.getText().toString());
		if (((TextView) findViewById(R.id.intervalwaterlevelunit)).getText()
				.toString().equals(hour)) {
			i = i * 60;
		}
		mAlarmManager.setWaterLevelAlarmInterval((int) i);

	}

	private void resetAlarmValue() {
		resetAlarmPh();
		resetAlarmWaterLevel();
		resetAlarmOxygen();
		resetAlarmTemperature();
		resetAlarmConductivity();
		resetPhoneEmail();

	}

	private void resetPhoneEmail() {
		mAlarmManager.setPhoneEnable(AlarmDefaultValue.ENABLEPHONE);
		mAlarmManager.setEmailEnable(AlarmDefaultValue.ENABLEEMAIL);
		mAlarmManager.setPhoneNum(AlarmDefaultValue.PHONENUM);
		mAlarmManager.setEmialAddr(AlarmDefaultValue.EMAILADDR);

	}

	private void resetAlarmPh() {
		mAlarmManager.setPhAlarmInterval(AlarmDefaultValue.INTERVALPH);
		mAlarmManager.setPhMax(AlarmDefaultValue.MAXPH);
		mAlarmManager.setPhMin(AlarmDefaultValue.MINPH);
	}

	private void resetAlarmWaterLevel() {
		mAlarmManager
				.setWaterLevelAlarmInterval(AlarmDefaultValue.INTERVALWATERLEVEL);
		mAlarmManager.setWaterLevelMax(AlarmDefaultValue.MAXWATERLEVEL);
		mAlarmManager.setWaterLevelMin(AlarmDefaultValue.MINWATERLEVEL);
	}

	private void resetAlarmOxygen() {
		mAlarmManager.setOxygenAlarmInterval(AlarmDefaultValue.INTERVALOxygen);
		mAlarmManager.setOxygenMax(AlarmDefaultValue.MAXOxygen);
		mAlarmManager.setOxygenMin(AlarmDefaultValue.MINOxygen);
	}

	private void resetAlarmTemperature() {
		mAlarmManager
				.setTemperatureAlarmInterval(AlarmDefaultValue.INTERVALTEMPERATURE);
		mAlarmManager.setTemperatureMax(AlarmDefaultValue.MAXTEMPERATURE);
		mAlarmManager.setTemperatureMin(AlarmDefaultValue.MINTEMPERATURE);
	}

	private void resetAlarmConductivity() {
		mAlarmManager
				.setConductivityAlarmInterval(AlarmDefaultValue.INTERVALConductivity);
		mAlarmManager.setConductivityMax(AlarmDefaultValue.MAXConductivity);
		mAlarmManager.setConductivityMin(AlarmDefaultValue.MINConductivity);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.resetall:
			resetAlarmValue();
			initAllView();
			break;
		case R.id.resetcondetivity:
			showConductivityDefault();
			break;
		case R.id.resetoxygen:
		showOxygenDefault();
			break;
		case R.id.resetph:
			showPhDefault();
			break;
		case R.id.resettemperature:
			showTemperatureDefault();
			break;
		case R.id.resetwaterlevel:
			showWaterLevelDefault();
			break;
		// case R.id.intervalcondutivityunit:
		case R.id.intervaloxygenunit:
			// case R.id.intervalphunit:
			// case R.id.intervalwaterlevelunit:
			// case R.id.intervaltemperatureunit:
			addPopupMenu(v);
			break;
		case R.id.ok:
			saveAllValue();
			this.finish();
			break;
		case R.id.cancel:
			this.finish();
			break;
		default:
			break;
		}
	}

	@SuppressLint("NewApi")
	public void addPopupMenu(final View v) {
		PopupMenu popup = new PopupMenu(this, v);
		popup.getMenuInflater().inflate(R.menu.minutehour, popup.getMenu());
		popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				CharSequence str = item.getTitle();
				((Button) v).setText(str);
				return true;
			}

		});

		popup.show();
	}

	// public void shoeDialog(String title, final int viewid) {
	// AlertDialog.Builder builder = new AlertDialog.Builder(this);
	// builder.setTitle(title);
	// final View view = getLayoutInflater().inflate(R.layout.alarmitem, null);
	// initDialogView(view, viewid);
	// // builder.setMessage("R.string.dialog_fire_missiles");
	// builder.setView(view);
	// builder.setPositiveButton(R.string.ok,
	// new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int id) {
	// setAlarm(view, viewid);
	// switch (id) {
	// case R.id.settingcondutivity:
	// setAlarm(view, id);
	// break;
	// case R.id.settingoxygen:
	// setAlarm(view, id);
	// break;
	// case R.id.settingph:
	// setAlarm(view, id);
	// break;
	// case R.id.settingtemperature:
	// setAlarm(view, id);
	// break;
	// case R.id.settingwaterlevel:
	// setAlarm(view, id);
	// break;
	//
	// default:
	// break;
	// }
	// // FIRE ZE MISSILES!
	// }
	// });
	// builder.setNegativeButton(R.string.cancel,
	// new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int id) {
	// // User cancelled the dialog
	// }
	// });
	// // Create the AlertDialog object and return it
	// builder.create();
	// builder.show();
	// }

	// private void initDialogView(View view, int viewid) {
	// EditText minEditText = (EditText) view.findViewById(R.id.min);
	// EditText maxEditText = (EditText) view.findViewById(R.id.max);
	// EditText intervalEditText = (EditText) view.findViewById(R.id.interval);
	// switch (viewid) {
	// case R.id.settingcondutivity:
	// minEditText.setText(mAlarmManager.getConductivityMin() + "");
	// maxEditText.setText(mAlarmManager.getConductivityMax() + "");
	// intervalEditText.setText(""
	// + (int) mAlarmManager.getConductivityAlarmInterval());
	// break;
	// case R.id.settingoxygen:
	// minEditText.setText(mAlarmManager.getOxygenMin() + "");
	// maxEditText.setText(mAlarmManager.getOxygenMax() + "");
	// intervalEditText.setText(""
	// + (int) mAlarmManager.getOxygenAlarmInterval());
	// break;
	// case R.id.settingph:
	// minEditText.setText(mAlarmManager.getPhMin() + "");
	// maxEditText.setText(mAlarmManager.getPhMax() + "");
	// intervalEditText.setText(""
	// + (int) mAlarmManager.getPhAlarmInterval());
	// break;
	// case R.id.settingtemperature:
	// minEditText.setText(mAlarmManager.getTemperatureMin() + "");
	// maxEditText.setText(mAlarmManager.getTemperatureMax() + "");
	// intervalEditText.setText(""
	// + (int) mAlarmManager.getTemperatureAlarmInterval());
	// break;
	// case R.id.settingwaterlevel:
	// minEditText.setText(mAlarmManager.getWaterLevelInterval() + "");
	// maxEditText.setText(mAlarmManager.getWaterLevelMax() + "");
	// intervalEditText.setText(""
	// + (int) mAlarmManager.getWaterLevelMin());
	// break;
	//
	// default:
	// break;
	// }
	//
	// }

	// public void setAlarm(View view, int id) {
	// float min;
	// float max;
	// int interval;
	// try {
	//
	// min = Float.valueOf(((EditText) view.findViewById(R.id.min))
	// .getText().toString().trim());
	// max = Float.valueOf(((EditText) view.findViewById(R.id.max))
	// .getText().toString().trim());
	// interval = (int) (float) Float.valueOf(((EditText) view
	// .findViewById(R.id.interval)).getText().toString().trim());
	// } catch (Exception e) {
	// Log.e("", "  valueof error");
	// return;
	// }
	// switch (id) {
	//
	// case R.id.settingcondutivity:
	// mAlarmManager.setConductivityMin(min);
	// mAlarmManager.setConductivityMax(max);
	// mAlarmManager.setConductivityAlarmInterval(interval);
	// initView();
	// break;
	// case R.id.settingoxygen:
	// mAlarmManager.setOxygenMin(min);
	// mAlarmManager.setOxygenMax(max);
	// mAlarmManager.setOxygenAlarmInterval(interval);
	// initView();
	// break;
	// case R.id.settingph:
	// mAlarmManager.setPhMin(min);
	// mAlarmManager.setPhMax(max);
	// mAlarmManager.setPhAlarmInterval(interval);
	// initView();
	// break;
	// case R.id.settingtemperature:
	// mAlarmManager.setTemperatureMin(min);
	// mAlarmManager.setTemperatureMax(max);
	// mAlarmManager.setTemperatureAlarmInterval(interval);
	// initView();
	// break;
	// case R.id.settingwaterlevel:
	// mAlarmManager.setWaterLevelAlarmInterval(interval);
	// mAlarmManager.setWaterLevelMax(max);
	// mAlarmManager.setWaterLevelMin(min);
	// initView();
	// break;
	//
	// default:
	// break;
	// }
	// }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		initAllView();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

	}

	@Override
	public void update(Observable observable, Object data) {

	}
}
